package com.lagou.edu.controller;

import com.lagou.edu.anno.CustomAutowired;
import com.lagou.edu.anno.CustomController;
import com.lagou.edu.anno.CustomRequestMapping;
import com.lagou.edu.anno.Security;
import com.lagou.edu.handler.ModelAndView;
import com.lagou.edu.pojo.User;
import com.lagou.edu.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Security(value = {"tom","lucy"})
@CustomController
@CustomRequestMapping(value = "/demo")
public class DemoController {

    @CustomAutowired(value = "userServiceImpl")
    private UserService service;

    @CustomRequestMapping(value = "/getUser")
    public ModelAndView getUser(HttpServletRequest request, HttpServletResponse response, String name, Integer age){
        User user = service.query(request, response, name, age);
        System.out.println("user ==> " + user);
        ModelAndView modelAndView = new ModelAndView();
        Map<String, Object> model = new HashMap<>();
        model.put("name", name);
        model.put("age", age);
        modelAndView.setModel(model);
        modelAndView.setView("/type.jsp");
        return modelAndView;
    }

    @Security(value = {"tom","lucy"})
    @CustomRequestMapping(value = "/getNewUser")
    public ModelAndView getNewUser(User user){
        User newUser = service.query(user);
        System.out.println("newUser ==> " + newUser);
        ModelAndView modelAndView = new ModelAndView();
        Map<String, Object> model = new HashMap<>();
        model.put("user", newUser);
        modelAndView.setModel(model);
        modelAndView.setView("/result.jsp");
        return modelAndView;
    }
}
